package com.zhida.chatservice.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zhida.chatservice.service.WsService;
import com.zhida.common.context.UserContext;
import com.zhida.common.entity.Result;
import com.zhida.common.entity.WsGroup;
import com.zhida.common.utils.TokenProcessor;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

/**
 * Create by zhh 2024/7/16 7:46
 */
@RestController
@RequestMapping("/ws")
public class WsController {

    @Resource
    final WsService wsService;

    public WsController(WsService wsService) {
        this.wsService = wsService;
    }

    @GetMapping("/userOnline/{username}")
    public Result<?> userOnline(@PathVariable String username){
        return wsService.userOnline(username);
    }
    @PostMapping("/createGroup")
    public Result<?> createGroup(@RequestBody WsGroup wsGroup){
        return wsService.createGroup(wsGroup,UserContext.getUser());
    }
    @GetMapping("/joinGroup/{group_id}")
    public Result<?> joinGroup(@PathVariable Integer group_id){
        return wsService.joinGroup(group_id, UserContext.getUser());
    }
    //  通过群加入申请
    @GetMapping("/applyJoinGroup/{group_id}/{username}")
    public Result<?> applyJoinGroup(@PathVariable Integer group_id,@PathVariable String username){
        return wsService.applyJoinGroup(group_id,username);
    }
    //  拒绝群加入申请
    @GetMapping("/refuseJoinGroup/{group_id}/{username}")
    public Result<?> refuseJoinGroup(@PathVariable Integer group_id,@PathVariable String username){
        return wsService.refuseJoinGroup(group_id,username);
    }
    //  踢出群聊
    @GetMapping("/kickOutGroup/{group_id}/{username}")
    public Result<?> kickOutGroup(@PathVariable Integer group_id,@PathVariable String username){
        return wsService.kickOutGroup(group_id,username);
    }
    //  开启群禁言
    @GetMapping("/groupMute/{group_id}")
    public Result<?> groupMute(@PathVariable Integer group_id){
        return wsService.groupMute(group_id);
    }
    //  关闭群禁言
    @GetMapping("/groupUnmute/{group_id}")
    public Result<?> groupUnmute(@PathVariable Integer group_id){
        return wsService.groupUnmute(group_id);
    }
    //  禁言群成员
    @PostMapping("/muteGroupMember")
    public Result<?> muteGroupMember(@RequestBody HashMap<String,String> muteData){
        return wsService.muteGroupMember(muteData);
    }
    //  解除禁言群成员
    @PostMapping("/unmuteGroupMember")
    public Result<?> unmuteGroupMember(@RequestBody HashMap<String,String> unmuteData){
        return wsService.unmuteGroupMember(unmuteData);
    }
    //  取群聊信息
    @GetMapping("/getGroupsInfo")
    public Result<?> getGroupsInfo(){
        return wsService.getGroupsInfo();
    }
    //  更新群信息
    @PostMapping("/updateGroupInfo")
    public Result<?> updateGroupInfo(@RequestBody HashMap<String,String> data){
        return wsService.updateGroupInfo(data);
    }
    //  取群成员列表
    @GetMapping("/getGroupMembers/{group_id}")
    public Result<?> getGroupMembers(@PathVariable String group_id){
        return wsService.getGroupMembers(group_id);
    }
    @PostMapping("/sendImages")
    public Result<?> sendImage(@RequestParam(value = "images",required = false) List<MultipartFile> files, @RequestParam(value="formData") String jsonFormData){
        return wsService.sendImages(files,jsonFormData);
    }
}
